﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DMS.Base;
using System.Data.Common;
using DMS.BaseClass;
using DMS.Business.Modules.Configuation;
using DMS.Business.Utility;
using DMS.BaseClass;

namespace DMS.Web.Modules.SystemManagement.Organization
{
    public partial class SystemSettingList : System.Web.UI.UserControl
    {
        #region 成员属性
        /// <summary>
        /// 页大小限制
        /// </summary>
        public int PageLimte
        {
            get
            {
                return this.pager.PageSize;
            }
            set
            {
                this.pager.PageSize = value;
            }
        }
        #endregion

        #region 界面事件
        /// <summary>
        /// 控件加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.InitDataList();
            }
        }
        /// <summary>
        /// 列表绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dgParamList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.AlternatingItem && e.Item.ItemType != ListItemType.Item)
            {
                return;
            }

            SysConfig config = e.Item.DataItem as SysConfig;
            if (config == null)
            {
                return;
            }

            Label lbParamType = e.Item.FindControl("lblParamType") as Label;
            if (lbParamType != null)
            {
                lbParamType.Text = config.Type == ParamTypes.Member ? "成员参数" : "系统参数";
            }
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void pager_PageChanged(object sender, EventArgs e)
        {
            this.InitDataList();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.pager.GoToPage(1);
            this.InitDataList();
        }
        #endregion

        #region 内部方法
        public void InitDataList()
        {
            int total = 0;
            string condition = string.Empty;

            List<DbParameter> dbParams = new List<DbParameter>();
            if (!string.IsNullOrEmpty(this.txtParamCode.Text))
            {
                condition += " AND [ParamID] LIKE '%@ParamID%'";
                DbParameter p_ParamID = DBHelper.CreateParamer();
                p_ParamID.ParameterName = "ParamID";
                p_ParamID.Value = this.txtParamCode.Text.Trim();
                dbParams.Add(p_ParamID);
            }

            if (!string.IsNullOrEmpty(this.txtParaName.Text))
            {
                condition += " AND [ParamName] LIKE '%@ParamName%'";
                DbParameter p_ParamName = DBHelper.CreateParamer();
                p_ParamName.ParameterName = "ParamName";
                p_ParamName.Value = this.txtParamCode.Text.Trim();
                dbParams.Add(p_ParamName);
            }

            if (!string.IsNullOrEmpty(this.ddlParamTypeList.SelectedValue))
            {
                condition += " AND [ParamType] = '@ParamType'";
                DbParameter p_ParamType = DBHelper.CreateParamer();
                p_ParamType.ParameterName = "ParamType";
                p_ParamType.Value = Convert.ToInt32((string) this.ddlParamTypeList.SelectedValue);
                dbParams.Add(p_ParamType);
            }

            List<SysConfig> configs = ConfigHelper.GetSysConfigs(pager.PageSize, pager.CurrentPageIndex, out total, condition, dbParams);
            configs = (from c in configs
                       where c.IsDisplay == true
                       select c).ToList();
            this.dgParamList.DataSource = configs;
            this.dgParamList.DataBind();

            this.pager.RecordCount = total;


            // 处理空数据
            if (total == 0)
            {
                this.dgParamList.Visible = false;
                this.pager.Visible = false;
                this.dvEmpty.Visible = true;
            }
            else
            {
                this.dgParamList.Visible = true;
                this.pager.Visible = true;
                this.dvEmpty.Visible = false;
            }
        }
        #endregion
    }
}